<?php

namespace App\HttpController\admin;


use App\Common\Admin\Admin;
use App\HttpController\ControllerBase;
use App\Model\t_t_user;
use EasySwoole\Component\Openssl;

class register extends ControllerBase
{
    //点击注册按钮，加入队列
    public function index()
    {
        $id = \h::funcGetID();


//        if (\h::funcIsDev())
//        {
//            $this->redis6380()->set(Admin::funcRegKeyCheckFix__redis($id), '', 30);
//        }
//        else
        {
            $this->redis()->lPush(Admin::funcRegKey__redis(), $id);
        }

        $this->commonR()->data['id'] = $id;


        $this->funcWriteCommonR();
    }


    //检查队列，成功走注册逻辑
    public function check_reg()
    {
        $id = $this->request()->getRequestParam('task_id');

        if (\h::funcStrIsNullOrEmpty($id))
        {
            return $this->funcWriteErr(-100, '缺少参数');
        }


        $r = $this->redis6380()->del(Admin::funcRegKeyCheckFix__redis($id));
        if ($r)
        {
            //pass
            $this->save();
        }
        else
        {
            return $this->funcWriteErr(-999, '等待');
        }


        $this->funcWriteCommonR();
    }


    //注册逻辑
    public function save()
    {
        $user_name = $this->request()->getRequestParam('user_name');
        $user_phone = $this->request()->getRequestParam('user_phone');
        $user_mail = $this->request()->getRequestParam('user_mail');
        $user_pwd = $this->request()->getRequestParam('user_pwd');
        $code = $this->request()->getRequestParam('code');


        //验证
        $v = new \EasySwoole\Validate\Validate();
        $v->addColumn('user_name')
            ->required('不能为空')
            ->lengthMax(50, '字符过长');
        $v->addColumn('user_phone')
            ->required('不能为空')
            ->lengthMax(20, '字符过长')
            ->func(function ($request_param, $key)
            {
                $val = $request_param[$key];

                $count = $this->db()->where('user_phone', $val)->count(t_t_user::table());
                if ($count <= 0)
                {
                    return true;
                }
                return false;
            }, '手机号码已注册');
        $v->addColumn('user_mail')
            ->required('不能为空')
            ->func(function ($request_param, $key)
            {
                $val = $request_param[$key];

                $count = $this->db()->where('user_mail', $val)->count(t_t_user::table());
                if ($count <= 0)
                {
                    return true;
                }
                return false;
            }, ' 邮箱已被使用');;
        $v->addColumn('user_pwd')->required('不能为空');
        $v->addColumn('code')->required('不能为空')
            ->func(function ($request_param, $key)
            {
                $val = $request_param[$key];

                $app = new Admin();

                return $app->funcCheckVCode($val);

            }, '验证码不正确');
        if (!$this->validate($v))
        {
            return $this->funcWriteValidate($v);
        }


        //保存
        $openssl = new Openssl(\h::funcKey('abc'));

        $entity_user = new t_t_user();
        $entity_user->user_name = $user_name;
        $entity_user->user_phone = $user_phone;
        $entity_user->user_mail = $user_mail;
        $entity_user->user_pwd = $openssl->encrypt($user_pwd);

        $r = $entity_user->funcInsert();
        if ($r)
        {
            //pass
        }
        else
        {
            $this->commonR()->errcode = -120;
            $this->commonR()->errmsg = '注册失败，系统异常';
        }
    }
}